package com.caopeng.achievementmanagement.controller;

import com.caopeng.achievementmanagement.pojo.Achievement;
import com.caopeng.achievementmanagement.service.AchievementService;
import com.caopeng.util.AliOssCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @author Crescent_P
 * @date 2021-12-05 11:05
 */
@Controller
public class AchievementController {
    @Autowired
    private AchievementService achievementService;

    // 删除成果
    @GetMapping("/delete")
    public String deleteAchievement(HttpSession session) {
        long id = (long) session.getAttribute("deleteId");

        int num = achievementService.deleteAchievement(id);
        return "redirect:toPersonalInfo";
    }

    // 上传结果
    @PostMapping("/uploadAchievement")
    public String uploadAchievement(Achievement achievement, @RequestParam("file") MultipartFile file) {

        String filename = file.getResource().getFilename();
        //这里文件名用了uuid 防止重复，可以根据自己的需要来写
        String name = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."), filename.length());
        name = name.replace("-", "");
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        AliOssCloudUtil util = new AliOssCloudUtil();
        //上传成功返回完整路径的url
        String url = util.uploadFile2OSS(inputStream, name);
        //存储的是支撑材料的文件名
        achievement.setFileUrl(name);
        int num = achievementService.uploadAchievement(achievement);
        return "redirect:toPersonalInfo";
    }

    // 更新结果
    @PostMapping("/updateAchievement")
    public String updateAchievement(Achievement achievement) {
        int num = achievementService.updateAchievement(achievement);
        return "redirect:toPersonalInfo";
    }

    /**
     * 下载oss对应文件
     *
     * @param fileUrl 储存的文件名
     * @return
     */
    @RequestMapping("/download/{fileUrl}")
    public void downloadFile(@PathVariable String fileUrl, HttpServletResponse response) {
        try {
            System.out.println(fileUrl);

            AliOssCloudUtil ossClientUtil = new AliOssCloudUtil();
            InputStream is = ossClientUtil.getInputStreamByFileUrl(ossClientUtil.getFiledir() + fileUrl);
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileUrl, "UTF-8"));
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = is.read(b)) != -1) {
                os.write(b, 0, len);
            }
            is.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
